<?php
require_once 'includes/auth.php';
require_once 'includes/config.php';
requireLogin();

$username = $_SESSION['username'];
$devInfo = getDeveloperInfo($username);

if (!$devInfo) {
    header('Location: index.php');
    exit;
}
require_once 'track_online.php';
$apiCount = getAPICount($username);
$regDate = new DateTime($devInfo['reg_time']);
$now = new DateTime();
$devDays = $now->diff($regDate)->days;
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>开发者控制台 - JIIL微聊</title>
    <link rel="stylesheet" href="css/style.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@400;500;700&display=swap" rel="stylesheet">
    <style>
        body {
            font-family: 'Noto Sans SC', sans-serif;
            background-color: #f5f7fa;
        }
        
        .console-container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 30px 20px;
        }
        
        .console-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 40px;
            padding-bottom: 20px;
            border-bottom: 1px solid #eaeaea;
        }
        
        .console-header h1 {
            font-size: 2.5rem;
            font-weight: 700;
            color: #2c3e50;
        }
        
        .user-info {
            font-size: 1.1rem;
            font-weight: 500;
            color: #3498db;
        }
        
        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
            gap: 25px;
            margin-bottom: 50px;
        }
        
        .stat-card {
            background: white;
            border-radius: 12px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
            padding: 30px;
            text-align: center;
            transition: transform 0.3s ease;
        }
        
        .stat-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.08);
        }
        
        .stat-label {
            font-size: 1.1rem;
            font-weight: 500;
            color: #6c757d;
            margin-bottom: 10px;
        }
        
        .stat-value {
            font-size: 3.5rem;
            font-weight: 700;
            color: #3498db;
            line-height: 1.2;
        }
        
        .stat-subtext {
            font-size: 0.95rem;
            color: #95a5a6;
            margin-top: 10px;
        }
        
        .auth-section {
            background: white;
            border-radius: 12px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
            padding: 40px;
            margin-bottom: 50px;
        }
        
        .section-title {
            font-size: 1.8rem;
            font-weight: 700;
            color: #2c3e50;
            margin-bottom: 30px;
            display: flex;
            align-items: center;
            gap: 15px;
        }
        
        .section-title::before {
            content: '';
            display: block;
            width: 6px;
            height: 30px;
            background: #3498db;
            border-radius: 3px;
        }
        
        .auth-id-container {
            position: relative;
            margin: 30px 0;
        }
        
        .auth-id-box {
            background: #f8fafc;
            border: 1px solid #e2e8f0;
            border-radius: 8px;
            padding: 20px;
            font-family: 'Courier New', monospace;
            font-size: 1.2rem;
            font-weight: 500;
            color: #2d3748;
            word-break: break-all;
            position: relative;
            padding-right: 70px;
        }
        
        .copy-btn {
            position: absolute;
            right: 20px;
            top: 50%;
            transform: translateY(-50%);
            background: #3498db;
            color: white;
            border: none;
            border-radius: 6px;
            padding: 8px 15px;
            font-size: 0.9rem;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s ease;
        }
        
        .copy-btn:hover {
            background: #2980b9;
        }
        
        .security-info {
    background: #f1f8fe;
    border-radius: 8px;
    padding: 25px;
    margin-top: 30px;
    color: #2c3e50; /* 添加深色文字 */
}

.security-title {
    font-size: 1.2rem;
    font-weight: 700;
    color: #2c3e50; /* 改为深色 */
    margin-bottom: 15px;
    display: flex;
    align-items: center;
    gap: 10px;
}


        .security-title::before {
            content: '⚠️';
        }
        
        .security-list {
            list-style-type: none;
            padding-left: 0;
        }
        
.security-list li {
    padding: 10px 0;
    padding-left: 30px;
    position: relative;
    border-bottom: 1px solid #e2e8f0;
    color: #2c3e50; /* 改为深色 */
}
        
        
        .security-list li:last-child {
            border-bottom: none;
        }
        
        .security-list li::before {
            content: '•';
            position: absolute;
            left: 10px;
            color: #3498db;
            font-weight: bold;
        }
    </style>
    <script>
        function copyAuthId() {
            const authId = document.getElementById('auth-id').textContent;
            navigator.clipboard.writeText(authId)
                .then(() => {
                    const btn = document.querySelector('.copy-btn');
                    btn.textContent = '已复制!';
                    setTimeout(() => {
                        btn.textContent = '复制ID';
                    }, 2000);
                })
                .catch(err => {
                    console.error('复制失败:', err);
                });
        }
    </script>
</head>
<body>
    <header class="top-bar">
        <div class="logo">JIIL微聊开发平台</div>
        <nav class="nav-links">
            <a href="index.php">首页</a>
            <a href="dev_console.php" class="active">控制台</a>
            <a href="dev_docs.php">文档</a>
            <a href="logout.php">退出</a>
        </nav>
    </header>
    
    <main class="console-container">
        <div class="console-header">
            <h1>开发者控制台</h1>
            <div class="user-info">欢迎，<?= $username ?></div>
        </div>
        
        <div class="stats-grid">
            <div class="stat-card">
                <div class="stat-label">API调用次数</div>
                <div class="stat-value"><?= $apiCount ?></div>
                <div class="stat-subtext">总使用量</div>
            </div>
            
            <div class="stat-card">
                <div class="stat-label">开发者天数</div>
                <div class="stat-value"><?= $devDays ?></div>
                <div class="stat-subtext">自 <?= date('Y-m-d', strtotime($devInfo['reg_time'])) ?></div>
            </div>
        </div>
        
        <section class="auth-section">
            <h2 class="section-title">API鉴权凭证</h2>
            
            <p>这是您访问API的认证凭证，请妥善保管并不要泄露给他人。</p>
            
            <div class="auth-id-container">
                <div id="auth-id" class="auth-id-box"><?= $devInfo['auth_id'] ?></div>
                <button class="copy-btn" onclick="copyAuthId()">复制ID</button>
            </div>
            
            <div class="security-info">
                <h3 class="security-title">安全提示</h3>
                <ul class="security-list">
                    <li>此ID是您账户的唯一凭证，请勿分享给他人</li>
                    <li>定期检查API调用记录，发现异常立即报告</li>
                    <li>不要将此ID硬编码在客户端代码中</li>
                    <li>如果怀疑泄露，请立即联系管理员重置</li>
                </ul>
            </div>
        </section>
    </main>
    
    <footer class="footer">
        <p>© <?= date('Y') ?> JIIL微聊开发者平台 版权所有</p>
    </footer>
</body>
</html>